/*
 * Copyright (C) 2000-2008 Jens Nixdorf and various contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies of the Software, its documentation and marketing & publicity
 * materials, and acknowledgment shall be given in the documentation, materials
 * and software packages that this Software was used.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package org.mopore.jat.timer.creation;

import static org.mopore.jat.timer.TimingType.COUNT_UP;
import static org.mopore.jat.timer.TimingType.CUSTOM_COUNT_DOWN;
import static org.mopore.jat.timer.TimingType.FOUR_DIGITS_COUNT_DOWN;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import org.mopore.jat.gui.wizard.AbstractWizardPage;
import org.mopore.jat.gui.wizard.ButtonPanel;
import org.mopore.jat.gui.wizard.WizardContext;
import org.mopore.jat.timer.TimingType;

public class StartPage extends AbstractWizardPage<TimingCreateProgress> {
	
	
	private TypeSetter typeSetter;
	
	
	private static final Map<TimingType, 
		Class<? extends AbstractWizardPage<TimingCreateProgress>>> navMap = 
			new HashMap<TimingType, 
			Class<? extends AbstractWizardPage<TimingCreateProgress>>>(){
		{
			put( FOUR_DIGITS_COUNT_DOWN, FourDigitsPage.class );
			put( CUSTOM_COUNT_DOWN, CustomCountDownPage.class );
			put( COUNT_UP, FinishPage.class );
		}
	};
	
	
	public StartPage() {
		
		ButtonPanel buttonPanel = getButtonPanel();
		buttonPanel.enableNextButton();
		buttonPanel.addActionToNextButton( new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				
				TimingType type = typeSetter.getTimingType();
				Class<? extends AbstractWizardPage<TimingCreateProgress>> pageClass =
					navMap.get( type );
				changePage( pageClass );
			}
		} );
	}


	@Override
	public JPanel getContentPanel(){
		
		TimingCreateProgress progress = context().get();
		TimingType type = progress.getNewCountDownType();
		
		typeSetter = new TypeSetter();
		typeSetter.setTimingType( type );
		
		JPanel panel = new JPanel();
		
		GridLayout gridLayout = new GridLayout( 4, 1 );
		panel.setLayout( gridLayout );
		
		final JLabel textLabel = new JLabel();
		textLabel.setText( "select type of new timing:" );
		panel.add( textLabel );
		
		final JRadioButton fourDigitsRadio = 
			new JRadioButton( "4 digits count down (hours and minutes)" );
		if ( FOUR_DIGITS_COUNT_DOWN == type ){
			fourDigitsRadio.setSelected( true );
		}
		panel.add( fourDigitsRadio );
		
		final JRadioButton customRadio = 
			new JRadioButton( "custom count down (seconds, minutes and hours)" );
		if ( CUSTOM_COUNT_DOWN == type ){
			customRadio.setSelected( true );
		}
		panel.add( customRadio );
		
		final JRadioButton countUpRadio = 
			new JRadioButton( "count up (stop watch function)" );
		if ( COUNT_UP == type ){
			countUpRadio.setSelected( true );
		}
		panel.add( countUpRadio );
		
		
		// add actions
		fourDigitsRadio.addActionListener( new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				customRadio.setSelected( false );
				countUpRadio.setSelected( false );
				ButtonPanel buttonPanel = getButtonPanel();
				buttonPanel.disableFinishButton();
				typeSetter.setTimingType( FOUR_DIGITS_COUNT_DOWN );
			}} );
		customRadio.addActionListener( new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				fourDigitsRadio.setSelected( false );
				countUpRadio.setSelected( false );
				ButtonPanel buttonPanel = getButtonPanel();
				buttonPanel.disableFinishButton();
				typeSetter.setTimingType( CUSTOM_COUNT_DOWN );
			}} );
		countUpRadio.addActionListener( new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				fourDigitsRadio.setSelected( false );
				customRadio.setSelected( false );
				ButtonPanel buttonPanel = getButtonPanel();
				buttonPanel.enableFinishButton();
				typeSetter.setTimingType( COUNT_UP );
			}} );
		
		panel.setBorder( BorderFactory.createEmptyBorder( 0, 10, 0, 10) );
		
		return panel;
	}


	@Override
	public void storeToContext() {
		
		WizardContext<TimingCreateProgress> context = context();
		TimingCreateProgress progress = context.get();
		
		TimingType type = typeSetter.getTimingType();
		progress.setType( type );
	}
	
	private class TypeSetter {
		
		private TimingType timingType;
		
		public TimingType getTimingType() {
			return timingType;
		}
		
		public void setTimingType(TimingType timingType) {
			this.timingType = timingType;
		}
	}
	

}
